package com.wg.dome.handle;

import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.attribute.AttributeMgr;
import com.wg.dome.entity.config.EquipLingShiEntity;
import com.wg.dome.entity.user.EquipEntity;
import com.wg.dome.entity.user.ItemEntity;
import com.wg.dome.operation.EquipLSQHRequest;
import com.wg.dome.operation.EquipLSQHResponse;
import com.wg.dome.repository.EquipLingShiRepository;
import com.wg.dome.repository.EquipRepository;
import com.wg.dome.repository.ItemRepository;
import com.wg.core.net.OperationRequest;
import com.wg.core.net.OperationResponse;
import com.wg.core.net.SendParameters;

/**
 * 装备灵石强化
 */
public class HandleRequest16 extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {

        EquipLSQHRequest equipLSQHRequest = new EquipLSQHRequest(request);

        if (!equipLSQHRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(equipLSQHRequest);
        }

        if (equipLSQHRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, equipLSQHRequest.getPlayerId(), peer.getPlayerId());
        }

        EquipRepository equipRepository = new EquipRepository();
        EquipEntity equipEntity = equipRepository.getEntity(equipLSQHRequest.getPlayerId(), equipLSQHRequest.getEquipId());
        if (equipEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.EQUIP_ERROR, equipLSQHRequest.getEquipId());
        }

        if (equipLSQHRequest.getIsType() < 0 || equipLSQHRequest.getIsType() >equipEntity.getLs().length) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.ITEM_ERROR);
        }

        short[] ls = equipEntity.getLs();
        short lsLv = ls[equipLSQHRequest.getIsType()];

        EquipLingShiRepository equipLingShiRepository = new EquipLingShiRepository();
        EquipLingShiEntity equipLingShiEntity = equipLingShiRepository.getNextEntity(lsLv);
        if (equipLingShiEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.LEVEL_MAX);
        }
        int itemId = equipLingShiEntity.getUseItemIds()[equipLSQHRequest.getIsType()];

        ItemRepository itemRepository = new ItemRepository();
        ItemEntity itemEntity = itemRepository.getItem(equipLSQHRequest.getPlayerId(), itemId);
        if (itemEntity == null || equipLingShiEntity.getUseItemCount() > itemEntity.getCount()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.ITEM_LIMIT, itemId);
        }

        int willUseCount = 0;
        int useCount = 0;
        short lsLevel = lsLv;

        for (short i = lsLevel; i < lsLevel + 5; i++) {
            EquipLingShiEntity entity = equipLingShiRepository.getNextEntity(i);
            if (entity == null){
                break;
            }
            willUseCount += entity.getUseItemCount();
            if (willUseCount > itemEntity.getCount()) {
                break;
            }
            useCount += entity.getUseItemCount();
            lsLv++;
        }

        ls[equipLSQHRequest.getIsType()] = lsLv;
        equipEntity.setLs(ls);
        equipRepository.update(equipEntity);

        itemEntity.setCount(itemEntity.getCount() - useCount);
        itemRepository.update(itemEntity);

        AttributeMgr.getFightAttribute(peer.getPlayerId(), equipEntity.getPos(), 1);

        EquipLSQHResponse response = new EquipLSQHResponse();
        response.setEquipId(equipLSQHRequest.getEquipId());
        response.setLevel(ls);
        response.setCount(itemEntity.getCount());
        response.setItemId(itemId);

        return new OperationResponse(request.getOperationCode(), response);
    }
}
